package com.tryine.soudafu.healty.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tryine.common.req.PageReq;
import com.tryine.common.resp.BaseResp;
import com.tryine.common.resp.PageBaseResp;
import com.tryine.soudafu.healty.api.HealtyProvider;
import com.tryine.soudafu.healty.vo.req.HealtyExpertTitleInfoReq;
import com.tryine.soudafu.healty.vo.req.base.BaseHealtyPageQueryReq;
import com.tryine.soudafu.healty.vo.req.base.BaseHealtyReq;
import com.tryine.soudafu.healty.vo.req.doc.HealtyExpertTitleReq;
import com.tryine.soudafu.healty.vo.req.healty.*;
import com.tryine.soudafu.healty.vo.resp.HealtyAuscultationInfoResp;
import com.tryine.soudafu.healty.vo.resp.HealtyExpertInfoResp;
import com.tryine.soudafu.healty.vo.resp.HealtyExpertTitleInfoResp;
import com.tryine.soudafu.healty.vo.resp.HealtyQuWeiInfo;
import com.tryine.soudafu.healty.vo.resp.detail.HealtyAuscultationDetailResp;
import com.tryine.soudafu.healty.vo.resp.detail.HealtyExpertDetailResp;
import com.tryine.soudafu.healty.vo.resp.detail.HealtyQuWeiDetailResp;
import com.tryine.soudafu.healty.vo.resp.hotSubject.HealtyHotSubjectResp;
import com.tryine.soudafu.healty.vo.resp.hotSubject.HealtyParentResp;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author ludans
 * @program soudafu-healty-controller
 * @date 2020/7/14 9:24
 */
@RestController
@RequestMapping(value = "/healty")
@Slf4j
@Api(description = "健康资讯相关API")
public class HealtyController {

    @Reference
    private HealtyProvider healtyProvider;

    /**
     * 权威期刊
     *
     * @return com.tryine.common.resp.BaseResp<com.tryine.soudafu.healty.vo.resp.HealtyQuWeiInfo>
     * @Author ludans
     * @Date: 2020/8/4 10:18
     * @MethodName: getPeriodical
     * @Param [req]
     **/
    @ApiOperation(value = "权威期刊")
    @RequestMapping(value = "getPeriodical", method = RequestMethod.GET)
    public BaseResp<HealtyQuWeiInfo> getPeriodical() {

        BaseResp<HealtyQuWeiInfo> periodical = healtyProvider.getPeriodical();
        return periodical;
    }

    @ApiOperation(value = "热门话题")
    @RequestMapping(value = "listHot", method = RequestMethod.POST)
    public PageBaseResp<List<HealtyHotSubjectResp>> listHot(@RequestBody(required = false) HealtyHotSubjectPageQuery query) {
        PageBaseResp<List<HealtyHotSubjectResp>> listBaseResp = healtyProvider.listHot(query);
        log.info("listResp:{}", listBaseResp);
        return listBaseResp;
    }

    /**
     * 典型医案  type : 12
     * 经典听诊  type : 13
     *
     * @return com.tryine.common.resp.BaseResp<com.tryine.soudafu.healty.vo.resp.HealtyAuscultationInfoResp>
     * @Author ludans
     * @Date: 2020/8/4 10:18
     * @MethodName: getAuscultation
     * @Param [req]
     **/
    @ApiOperation(value = "经典听诊和典型医案", notes = "典型医案 ， 类型传 12 ；  \n" +
            " 经典听诊 ，类型传 13")
    @RequestMapping(value = "getAuscultation", method = RequestMethod.POST)
    public BaseResp<HealtyAuscultationInfoResp> getAuscultation(@RequestBody BaseHealtyReq req) {
        Assert.notNull(req.getInfoType(), "InfoType参数不能为空!");
        BaseResp<HealtyAuscultationInfoResp> auscultation = healtyProvider.getAuscultation(req);
        return auscultation;
    }

    /**
     * 学习园地 --- type  3
     * 专家专刊 --- type 4
     *
     * @return com.tryine.common.resp.BaseResp<com.tryine.soudafu.healty.vo.resp.HealtyExpertInfoResp>
     * @Author ludans
     * @Date: 2020/8/4 10:19
     * @MethodName: getExpertInfo
     * @Param [req]
     **/
    @ApiOperation(value = "学习园地和专家专刊", notes = "增加了标题搜索功能 \n 学习园地， 类型传 3 ；  \n" +
            "专家专刊，类型传 11")
    @RequestMapping(value = "getExpertInfo", method = RequestMethod.POST)
    public BaseResp<HealtyExpertInfoResp> getExpertInfo(@RequestBody BaseHealtyReq req) {
        Assert.notNull(req.getInfoType(), "InfoType参数不能为空!");
        req.setInfoStatus(1);
        BaseResp<HealtyExpertInfoResp> expertInfo = healtyProvider.getExpertInfo(req);
        return expertInfo;
    }

    @ApiIgnore
    @ApiOperation(value = "专家文章", notes = "此处的作者Id 与 医生ID 相对应。   \n 资讯类型传 4 ，platform 传 0\n" +
            "这个方法，暂时废弃，需要与医生ID相关联。")
    @RequestMapping(value = "listExpertTitle", method = RequestMethod.POST)
    public PageBaseResp<List<HealtyExpertTitleInfoResp>> listExpertInfo(@RequestBody HealtyExpertTitleInfoReq req) {
        Assert.notNull(req.getInfoType(), "InfoType参数不能为空!");
        PageBaseResp<List<HealtyExpertTitleInfoResp>> expertInfo = healtyProvider.listExpertTitleInfo(req);
        return expertInfo;
    }

    @ApiIgnore
    @ApiOperation(value = "添加专家文章", notes = "添加专家文章\n" +
            "暂时忽略")
    @RequestMapping(value = "addExpertTitle", method = RequestMethod.POST)
    public BaseResp getExpertInfo(@RequestBody HealtyExpertTitleReq req) {
        BaseResp baseResp = healtyProvider.addExpertTitle(req);
        return baseResp;
    }


    /**
     * 点赞
     *
     * @param userId
     * @param postId
     * @param type
     * @return
     */
    @ApiOperation(value = "点赞/收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID", name = "userId", required = true, paramType = "query"),
            @ApiImplicitParam(value = "帖子ID", name = "postId", required = true, paramType = "query"),
            @ApiImplicitParam(value = "类型<ul><li>0-点赞</li><li>1-收藏</li>", name = "type", required = true, paramType = "query")
    })
    @RequestMapping(value = "doLike", method = RequestMethod.GET)
    public BaseResp doLike(@RequestParam Long userId, @RequestParam Long postId, @RequestParam Integer type) {
        HealtyUserLikeReq req = buildUserLike(userId, postId, type);
        return healtyProvider.doLike(req);
    }

    @ApiOperation(value = "取消收藏 / 取消点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户ID", name = "userId", required = true, paramType = "query"),
            @ApiImplicitParam(value = "帖子ID", name = "postId", required = true, paramType = "query"),
            @ApiImplicitParam(value = "类型<ul><li>0-点赞</li><li>1-收藏</li>", name = "type", required = true, paramType = "query")
    })
    @RequestMapping(value = "cancelLike", method = RequestMethod.GET)
    public BaseResp cancelLike(@RequestParam Long userId, @RequestParam Long postId, @RequestParam Integer type) {
        HealtyUserLikeReq req = buildUserLike(userId, postId, type);
        return healtyProvider.cancelLike(req);
    }

    @ApiOperation(value = "热门话题详情页(二级页面）", notes = "ParentId -- 父Id,热门话题ID ；  \n" +
            "UserId --  用户ID ")
    @RequestMapping(value = "listHotSubject", method = RequestMethod.POST)
    public BaseResp<HealtyParentResp> listHostSubject(@RequestBody HealtyHotSubjectReq req) {
        Assert.notNull(req.getParentId(), "ParentId 不能为空！");
        Assert.notNull(req.getUserId(), "UserId 不能为空！");
        BaseResp<HealtyParentResp> infos = healtyProvider.listHostSubject(req);
        log.info("热门话题 父话题名称:{}", infos.getData().getPName());
//        log.info("子话题 1：{} ", infos.getData().getSubjectRespsInfos().get(0));
        return infos;
    }


    @ApiOperation(value = "关注/取消关注", notes = "关注传 1 ， 取消关注 - 0 ")
    @RequestMapping(value = "doFollow", method = RequestMethod.POST)
    public BaseResp doFollow(@RequestBody HealtyUserFollowReq req) {
        Assert.notNull(req.getFollow(), "类型不能为空！");
        Assert.notNull(req.getFollowedUsersId(), "发布者Id不能为空！");
        Assert.notNull(req.getUserId(), "用户ID 不能为空！");
        return healtyProvider.doFollow(req);
    }

    @ApiOperation(value = "经典听诊 列表", notes = "添加了标题模糊查询功能  \n" +
            "type: 经典听诊 - 13 ;  \n" +
            "典型医案 - 12  \n" +
            "经典听诊是 患者端 展示的内容 platform - 0   \n" +
            "典型医案是 医生端展示的内容 platform - 1 ")
    @RequestMapping(value = "listAuscultation", method = RequestMethod.POST)
    public PageBaseResp<List<HealtyAuscultationInfoResp>> listAuscultation(@RequestBody BaseHealtyPageQueryReq req) {
        Assert.notNull(req.getInfoType(), "资讯类型不能为空！");
        return healtyProvider.listAuscultation(req);
    }


    @ApiOperation(value = "专家专刊 列表", notes = "根据标题模糊查询功能" +
            "type: 专家专刊 - 11 ;  \n" +
            "学习园地 - 3  \n" +
            "专家专刊是 患者端 展示的内容 platform - 0   \n" +
            "学习园地是 医生端展示的内容 platform - 1 ")
    @RequestMapping(value = "listExpertInfo", method = RequestMethod.POST)
    PageBaseResp<List<HealtyExpertInfoResp>> listExpertInfo(@RequestBody BaseHealtyPageQueryReq req) {
        Assert.notNull(req.getInfoType(), "资讯类型不能为空！");
        return healtyProvider.listExpertInfo(req);
    }

    @ApiOperation(value = "权威期刊 列表", notes = "增加了标题的模糊查询，权威期刊的参数不是必传，这些参数仅仅只是传递分页参数")
    @RequestMapping(value = "listPeriodical", method = RequestMethod.POST)
    public PageBaseResp<List<HealtyQuWeiInfo>> listPeriodical(@RequestBody(required = false) HealtyQuWeiPageQuery reqs) {
        return healtyProvider.listPeriodical(reqs);
    }


//    @ApiIgnore
//    @ApiOperation(value = "专家专刊 / 学习园地 详情", notes = "H5 调这个接口 ，获取详细情况  \n" +
//            "专家专刊测试ID: 3   ;   \n 学习园地测试ID: 24")
//    @ApiImplicitParam(value = "id", name = "id", required = true, paramType = "query", example = "3")
//    @RequestMapping(value = "detailExpert", method = RequestMethod.GET)
//    public BaseResp<HealtyExpertDetailResp> detailExpert(@RequestParam(required = true) Long id) {
//        Assert.notNull(id, "Id不能为空");
//
//        return healtyProvider.detailExpert(id);
//    }


    @ApiIgnore
    @ApiOperation(value = "经典听诊 / 典型医案 /专家专刊 / 学习园地 详情 ", notes = "H5 调这个接口 ，获取详细情况    \n" +
            "经典听诊测试 Id : 2  ;   \n  典型测试ID: 27" +
            "专家专刊测试ID: 3   ;   \n 学习园地测试ID: 24")
    @ApiImplicitParam(value = "id", name = "id", required = true, paramType = "query", example = "30")
    @RequestMapping(value = "detailInfos", method = RequestMethod.GET)
    public BaseResp<HealtyExpertDetailResp> detailInfos(@RequestParam(required = true) Long id) {
        Assert.notNull(id, "Id不能为空");
        return healtyProvider.detailInfos(id);
    }


    @ApiIgnore
    @ApiOperation(value = "权威期刊 详情", notes = "H5 调这个接口 ，获取详细情况     ;\n  权威期刊测试ID: 1 ")
    @ApiImplicitParam(value = "id", name = "id", required = true, paramType = "query", example = "1")
    @RequestMapping(value = "detailPeriodical", method = RequestMethod.GET)
    public BaseResp<HealtyQuWeiDetailResp> detailPeriodical(@RequestParam(required = true) Long id) {
        Assert.notNull(id, "Id不能为空");
        return healtyProvider.detailPeriodical(id);
    }

//    @ApiIgnore
//    @ApiOperation(value = "经典听诊 / 典型医案详情", notes = "H5 调这个接口 ，获取详细情况    \n" +
//            "经典听诊测试 Id : 2  ;   \n  典型测试ID: 27")
//    @ApiImplicitParam(value = "id", name = "id", required = true, paramType = "query", example = "30")
//    @RequestMapping(value = "detailAuscultation", method = RequestMethod.GET)
//    public BaseResp<HealtyAuscultationDetailResp> detailAuscultation(@RequestParam(required = true) Long id) {
//        Assert.notNull(id, "Id不能为空");
//        return healtyProvider.detailAuscultation(id);
//    }

    private HealtyUserLikeReq buildUserLike(Long userId, Long postId, Integer type) {
        Assert.notNull(postId, "PostId不能为空！");
        Assert.notNull(userId, "UserId不能为空！");
        Assert.notNull(type, "Type不能为空！");
        HealtyUserLikeReq req = new HealtyUserLikeReq();
        req.setLikedPostId(postId);
        req.setLikedUserId(userId);
        req.setUserLikeType(type);
        return req;
    }

}
